package com.iwinding.ladygg.ui.act.device;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.iwinding.ladygg.R;
import com.iwinding.ladygg.bluetooth.impblekit.BleKitManager;
import com.iwinding.ladygg.bluetooth.impblekit.ICaneBleApi;
import com.iwinding.ladygg.ui.act.BaseActivity;
import com.iwinding.ladygg.ui.act.WheelActivity;

import java.util.ArrayList;

/**
 * Created by hezhiyong on 2017/5/17.
 */

public class DeviceDetailActivity extends BaseActivity {

    private final static int SELECT_VOLME = 100;

    public static final String UUID = "UUID";
    public static final String NAME = "name";
    public static final String RSSI = "rssi";
    public static final String VOLUME = "volume";

    private String uuid;
    private String name;
    private String rssi;
    private int volume;

    private View lay_volume;
    private TextView mTvUUID;
    private TextView mTvName;
    private TextView mTvRssi;
    private TextView mTvVolume;

    @Override
    protected int getLayoutID() {
        return R.layout.icane_activity_device_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    void initView() {
        setTitleResId(R.string.icane_device_detail);

        lay_volume = findViewById(R.id.lay_device_volume);
        mTvUUID = (TextView) findViewById(R.id.text_device_uuid);
        mTvName = (TextView) findViewById(R.id.text_device_name);
        mTvRssi = (TextView) findViewById(R.id.text_device_rssi);
        mTvVolume = (TextView) findViewById(R.id.text_device_volume);

        lay_volume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, WheelActivity.class);
                intent.putExtra(WheelActivity.WHEELSELECTSTR, String.valueOf(volume));
                ArrayList<String> data = new ArrayList<String>();
                data.add("0");
                data.add("1");
                data.add("2");
                data.add("3");
                data.add("4");
                data.add("5");
                intent.putExtra(WheelActivity.WHEELSELECTDATA, data);
                startActivityForResult(intent, SELECT_VOLME);
            }
        });
    }

    void initData() {
        uuid = getIntent().getStringExtra(UUID);
        name = getIntent().getStringExtra(NAME);
        rssi = getIntent().getStringExtra(RSSI);
        volume = getIntent().getIntExtra(VOLUME, -1);

        mTvUUID.setText(uuid);
        mTvName.setText(name);
        mTvRssi.setText(rssi);
        mTvVolume.setText(String.valueOf(volume));
        if (volume == -1)
            lay_volume.setVisibility(View.GONE);
    }

    void setVolume(int volume) {
        byte[] cmd = ICaneBleApi.getInstance().cmdVolume((byte) volume);
        ICaneBleApi.getInstance().setVolume(volume);
        String serviceName = ICaneBleApi.getInstance().getServiceName();
        String serviceFeatures = ICaneBleApi.getInstance().getServiceFeatures();
        BleKitManager.getInstance().writeBle(serviceName, serviceFeatures, cmd);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_VOLME && resultCode == RESULT_OK) {
            String result = data.getStringExtra(WheelActivity.WHEELSELECTSTR);
            volume = Integer.parseInt(result);
            setVolume(volume);
            mTvVolume.setText(String.valueOf(volume));
        }
    }
}
